package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.SearchListAdapter;

/**
 * 搜索界面
 * 
 * @author lkx Created at 2015-10-6 下午5:30:44
 * @version 1.0
 */
public class SearchActivity extends BaseActivity implements OnCheckedChangeListener {
	static final String TAG = "SearchActivity";
	private RadioButton infomation, farm_machinery, encyclopedia;
	private ListView searchDataList;
	private SearchListAdapter adapter;

	EditText etKeyword;
	JSONArray array;
	int type = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_search);
		initView();
	}

	public void initView() {

		adapter = new SearchListAdapter(SearchActivity.this);
		infomation = (RadioButton) findViewById(R.id.inforamtion);
		farm_machinery = (RadioButton) findViewById(R.id.farm_machinery);
		encyclopedia = (RadioButton) findViewById(R.id.encyclopedia);
		searchDataList = (ListView) findViewById(R.id.searchList);
		etKeyword = (EditText) findViewById(R.id.etKeyword);

		etKeyword.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView tv, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					search();
				}
				return false;
			}
		});

		infomation.setOnCheckedChangeListener(this);
		farm_machinery.setOnCheckedChangeListener(this);
		encyclopedia.setOnCheckedChangeListener(this);
		findViewById(R.id.btnBack).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		findViewById(R.id.btnSearch).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				search();
			}
		});
		infomation.setChecked(true);
		searchDataList.setAdapter(adapter);
		searchDataList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					etKeyword.setText(array.getJSONObject(arg2).getString("key"));
					search();
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);
				}
			}
		});
		load();
	}

	void search() {
		String key = etKeyword.getText().toString();
		if (TextUtils.isEmpty(key)) {
			ToastKit.showShort(this, "请输入您要搜索的内容");
			etKeyword.requestFocus();
			return;
		}
		Intent intent = new Intent(this, SearchResultActivity.class);
		intent.putExtra("key", key);
		startActivity(intent);
	}

	void load() {
		Api.get("searchKeyWord", ApiParam.create().addParam("type", type), new ApiCallback(this, true) {

			@Override
			public void handleFailure(Exception e, String msg) {
			}

			@Override
			public void handleSuccess(ApiResult result) {
				array = adapter.set(result.getDatas());
				adapter.notifyDataSetChanged();
			}

		}, this);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.inforamtion:
				findViewById(R.id.inforamtionLine).setVisibility(View.VISIBLE);
				findViewById(R.id.farm_machineryLine).setVisibility(View.INVISIBLE);
				findViewById(R.id.encyclopediaLine).setVisibility(View.INVISIBLE);
				type = 1;
				break;
			case R.id.farm_machinery:
				findViewById(R.id.inforamtionLine).setVisibility(View.INVISIBLE);
				findViewById(R.id.farm_machineryLine).setVisibility(View.VISIBLE);
				findViewById(R.id.encyclopediaLine).setVisibility(View.INVISIBLE);
				type = 2;
				break;
			case R.id.encyclopedia:
				findViewById(R.id.inforamtionLine).setVisibility(View.INVISIBLE);
				findViewById(R.id.farm_machineryLine).setVisibility(View.INVISIBLE);
				findViewById(R.id.encyclopediaLine).setVisibility(View.VISIBLE);
				type = 3;
				break;
			}
			load();
		}
	}
}
